// JavaScript Document
$(function(){
	var pager = $('#dg').datagrid().datagrid('getPager');    // get the pager of datagrid
	pager.pagination({
		buttons:[{
			iconCls:'icon-add',
			handler:function(){
				newItem();
			}
		}/*,{
			iconCls:'icon-edit',
			handler:function(){
				editItem();
			}
			
		},{
			iconCls:'icon-remove',
			handler:function(){
				delItem();
			}
		}*/]

	});            
});
var saveUrl;
//表单搜索
function searchItem(){
	var params = getParams();
	$('#dg').datagrid('load',params); 
}
//弹出增加表单
function newItem(){
	saveUrl = doUrl;
	$('#dlg').dialog('open').dialog('setTitle','新增'+dealEntity);
	$('#fm').form('clear');
	if(typeof(editor) != "undefined")
	{
		editor.html('');
	}
}
//弹出编辑表单
function editItem(){
	var row = $('#dg').datagrid('getSelected');
	if (row){
		$('#dlg').dialog('open').dialog('setTitle','编辑'+dealEntity);
		$('#fm').form('load',row);
		//其它初始化
		if($.isFunction(window.initOther))
		{
			initOther(row);	
		}
		saveUrl = doUrl + '?id='+row.pk_id;
	}
}
//保存新增和编辑
function saveItem(){
	$('#fm').form('submit',{
		url: saveUrl,
		onSubmit: function(){
			//其它初始化
			if($.isFunction(window.initOther))
			{
				initOther();	
			}
			return $(this).form('validate');
		},
		success: function(result){
			var json = $.parseJSON(result);
			if (json.status == 1){
				$('#dlg').dialog('close');      
				$('#dg').datagrid('reload');  
			} else {
				$.messager.alert('错误提示','操作出错！');
			}
		}
	});
}
//删除操作
function delItem(){
	var row = $('#dg').datagrid('getSelected');
	if (row){
		$.messager.confirm('确认提示','删除将不可恢复，确认删除？',function(r){
			if (r){
				$.post(delUrl,{id:row.pk_id},function(result){
					var json = $.parseJSON(result);
					if (json.status == 1){
						$('#dg').datagrid('reload');    
					} else {
						$.messager.alert('错误提示','操作出错！');
					}
				},'text');
			}
		});
	}
}